home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / TESTNDBM.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  6KB  |  227 lines

  1. /* testndbm.c - Driver program to test the ndbm interface routines. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/testndbm.c'v 1.4.0.1 90/08/16 09:22:51 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53. #ifdef SYSV
  54. #include <fcntl.h>
  55. #endif
  56. #ifdef GNU
  57. #include "ndbm.h"
  58. #else
  59. #include <ndbm.h>
  60. #endif
  61.  
  62. #define TRUE  1
  63. #define FALSE 0
  64.  
  65.  
  66. /* The test program allows one to call all the routines plus the hash function.
  67.    The commands are single letter commands.  The user is prompted for all other
  68.    information.  The commands are q (quit), f (fetch), s (store), d (delete),
  69.    1 (firstkey), n (nextkey) and h (hash function). */
  70.  
  71. main (argc, argv)
  72.      int argc;
  73.      char *argv[];
  74. {
  75.  
  76.   char  cmd_ch;
  77.  
  78.   datum key_data;
  79.   datum data_data;
  80.   datum return_data;
  81.  
  82.   char key_line[500];
  83.   char data_line[1000];
  84.  
  85.   DBM *dbm_file;
  86.  
  87.   char done = FALSE;
  88.  
  89.   char *file_name;
  90.  
  91.   /* Argument checking. */
  92.   if (argc > 2)
  93.     {
  94.       printf ("Usage: %s [dbm-file] \n",argv[0]);
  95.       exit (2);
  96.     }
  97.  
  98.   if (argc > 1)
  99.     {
  100.       file_name = argv[1];
  101.     }
  102.   else
  103.     {
  104.       file_name = "junkndbm";
  105.     }
  106.  
  107.   /* Initialize. */
  108.   data_data.dptr = data_line;
  109.  
  110.   dbm_file = dbm_open (file_name, O_RDWR|O_CREAT, 00664);
  111.   if (dbm_file == NULL)
  112.     {
  113.       printf ("dbm_open failed.\n");
  114.       exit (2);
  115.     }
  116.  
  117.   /* Welcome message. */
  118.   printf ("\nWelcome to the gndbm test program.  Type ? for help.\n\n");
  119.   
  120.   while (!done)
  121.     {
  122.       printf ("com -> ");
  123.       cmd_ch = getchar ();
  124.       while (getchar () != '\n') /* Do nothing. */;
  125.       switch (cmd_ch)
  126.     {
  127.     case 'q':
  128.       done = TRUE;
  129.       break;
  130.  
  131.     case 'f':
  132.       printf ("key -> ");
  133.       gets (key_line);
  134.       key_data.dptr = key_line;
  135.       key_data.dsize = strlen (key_line)+1;
  136.       return_data = dbm_fetch (dbm_file, key_data);
  137.       if (return_data.dptr != NULL)
  138.           printf ("data is ->%s\n\n", return_data.dptr);
  139.       else
  140.         printf ("No such item found.\n\n");
  141.       break;
  142.  
  143.     case 's':
  144.       printf ("key -> ");
  145.       gets (key_line);
  146.       key_data.dptr = key_line;
  147.       key_data.dsize = strlen (key_line)+1;
  148.       printf ("data -> ");
  149.       gets (data_line);
  150.       data_data.dsize = strlen (data_line)+1;
  151.       if (dbm_store (dbm_file, key_data, data_data, DBM_REPLACE) != 0)
  152.         printf ("Item not inserted. \n");
  153.       printf ("\n");
  154.       break;
  155.  
  156.     case 'd':
  157.       printf ("key -> ");
  158.       gets (key_line);
  159.       key_data.dptr = key_line;
  160.       key_data.dsize = strlen (key_line)+1;
  161.       if (dbm_delete (dbm_file, key_data) != 0)
  162.         printf ("Item not found or deleted\n");
  163.       printf ("\n");
  164.       break;
  165.  
  166.     case '1':
  167.       key_data = dbm_firstkey (dbm_file);
  168.       if (key_data.dptr != NULL)
  169.         {
  170.           return_data = dbm_fetch (dbm_file, key_data);
  171.           printf ("key is ->%s\n", key_data.dptr);
  172.           printf ("data is ->%s\n\n", return_data.dptr);
  173.         }
  174.       else
  175.         printf ("No such item found.\n\n");
  176.       break;
  177.  
  178.  
  179.     case '2':
  180.       key_data = dbm_nextkey (dbm_file);
  181.       if (key_data.dptr != NULL)
  182.         {
  183.           return_data = dbm_fetch (dbm_file, key_data);
  184.           printf ("key is ->%s\n", key_data.dptr);
  185.           printf ("data is ->%s\n\n", return_data.dptr);
  186.         }
  187.       else
  188.         printf ("No such item found.\n\n");
  189.       break;
  190.  
  191.     case 'c':
  192.       {
  193.         int temp;
  194.         temp = 0;
  195.         return_data = dbm_firstkey (dbm_file);
  196.         while (return_data.dptr != NULL)
  197.           {
  198.         temp++;
  199.         return_data = dbm_nextkey (dbm_file);
  200.           }
  201.         printf ("There are %d items in the database.\n\n", temp);
  202.       }
  203.       break;
  204.  
  205.     case '?':
  206.       printf ("c - count elements\n");
  207.       printf ("d - delete\n");
  208.       printf ("f - fetch\n");
  209.       printf ("q - quit\n");
  210.       printf ("s - store\n");
  211.       printf ("1 - firstkey\n");
  212.       printf ("2 - nextkey on last return value\n\n");
  213.       break;
  214.  
  215.     default:
  216.       printf ("What? \n\n");
  217.       break;
  218.  
  219.     }
  220.     }
  221.  
  222.   /* Quit normally. */
  223.   dbm_close (dbm_file);
  224.   exit (0);
  225.  
  226. }
  227.